<%@page import="SERVLET.ObraServlet"%>
<%@page import="DTO.ObraDTO"%>
<%@page import="DTO.AutorDTO"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@page import="DTO.AutorLivroDTO"%>
<%@page contentType="text/html" pageEncoding="UTF-8" language="java" session="true"%>
<!DOCTYPE html>

<%
	List<AutorDTO> autores = (List<AutorDTO>) session.getAttribute("autores");
	if(autores == null){
		autores = new ArrayList<AutorDTO>();
	}
	List<AutorDTO> autoresObra = (List<AutorDTO>) session.getAttribute("autoresObra");	
	
	ObraDTO obraDTO = (ObraDTO) session.getAttribute("obraDTO");
%>

<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>Cadastro: Obra</title>
		<link rel="stylesheet" type="text/css" href="../../../livros/styles/styles.css" />
		<script type="text/javascript" src="../../livros/js/view.js"></script>
		<script type="text/javascript" src="../../livros/js/validacao.js"></script>
	</head>
	<body>
		<jsp:include page="../include/header.jsp"></jsp:include>

		<div id="conteudo">
			<div id="conteudoPagina">
				<h1>Cadastro de Obra</h1>
				<span class="warningsSpam">
					<h4 class="camposObrigatorios">* = Campos Obrigatórios</h4>			
					<% 		
				 	if (session.getAttribute("erro") != null) {
				 		out.println(session.getAttribute("erro"));
				 	} %>
			 	</span>
				<form action="ObraServlet" method="post" onsubmit="return validarObra(this)" enctype="multipart/form-data">
					<table>				
						<tr>
							<td>Titulo:<span class="warningsSpam">*</span></td>
							<td><input type="text" name="titulo" id="titulo"
								value="<%=obraDTO.getTitulo() != null ? obraDTO.getTitulo() : ""%>">
							</td>
						</tr>
						<tr>
							<td>Sinopse:<span class="warningsSpam">*</span></td>
							<td>
								<textarea name="descricao" id="descricao" style="width: 500px; height: 80px"><%=obraDTO.getDescricao() != null ? obraDTO.getDescricao() : ""%></textarea>
							</td>
						</tr>
						<tr>
							<td>Páginas:<span class="warningsSpam">*</span></td>
							<td><input type="text" name="paginas" id="paginas"
								value="<%=obraDTO.getPaginas() != 0 ? obraDTO.getPaginas() : ""%>">
							</td>
						</tr>
						<tr>
							<td>Edição:</td>
							<td><input type="text" name="edicao" id="edicao"
								value="<%=obraDTO.getEdicao() != null ? obraDTO.getEdicao() : ""%>">
							</td>
						</tr>
						<tr>
							<td>Editora:</td>
							<td><input type="text" name="editora" id="editora"
								value="<%=obraDTO.getEditora() != null ? obraDTO.getEditora() : ""%>">
							</td>
						</tr>
						<tr>
							<td>Capa:</td>
							<td><input type="file" name="capa" id="capa" />
							</td>
						</tr>
						<tr>
							<td>Ano:</td>
							<td><input type="text" name="ano" id="ano"
								value="<%=obraDTO.getAno() != null ? obraDTO.getAno() : ""%>">
							</td>
						</tr>
						<tr>
							<td>Autores: </td>																															
							<td>
								<select multiple="multiple" name="autores" id="autores" size="8" style="width: 60%">
								<% for(AutorDTO a : autores) { %>                 
					                <option value="<%=a.getCodigo()%>"><%=a.getNome()%></option>
					            <% } %>			                	
		            			</select>
	            										
							<%if(autoresObra != null) {%>														
								<table style="float: right; margin-right:10px; margin-top: 10px">
									<th colspan="2">Atuais da Obra</th>
									<%for(AutorDTO a : autoresObra) {%>
									<tr>
										<td><%=a.getNome() %></td>
										<td>
											<a href="ObraServlet?acao=ExcluirAutor&id=<%=a.getCodigo()%>"id="acao">
												<img alt="X" src="../../livros/img/delete2.png" />
											</a>
										</td>
									</tr>									
									<% } %>			                										
								</table>												
							<% } %>
							</td>	
	            		</tr>
	            		<tr>
	            			<td></td>            		
            				<td>
								<a href="ObraServlet?acao=AdicionarAutor"id="acao">Adicionar Autor</a>
							</td>
	            		</tr>
					</table>
					<button type="submit" name="acao" id="acao" value="Incluir" class="botao">Incluir</button>
				</form>
				<form action="ObraServlet" method="post">
					<button type="submit" name="acao" id="acao" value="Cancelar" class="botao">Cancelar</button>
				</form>
			</div>
			<jsp:include page="../include/destaqueAutores.jsp"></jsp:include>
		</div>
	</body>
</html>